home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4362 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  79 lines

  1. Newsgroups: comp.lang.c
  2. Path: mxsld2.pd.infn.it!LORETI
  3. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  4. Subject: Re: The size of a file
  5. X-Nntp-Posting-Host: mxsld2.pd.infn.it
  6. Message-ID: <DM85t1.2rq@news.cern.ch>
  7. Sender: news@news.cern.ch (USENET News System)
  8. Reply-To: loreti@mxsld2.pd.infn.it
  9. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  10. References: <4ebc03$4gv@gate.compart.fi>,<11a7cc$142013.177@news.comet.net>
  11. Date: Sun, 4 Feb 1996 00:10:10 GMT
  12.  
  13. In article <11a7cc$142013.177@news.comet.net>, mwilliams1@comet.net writes:
  14. >In <4ebc03$4gv@gate.compart.fi>, Fredrik Sandstrom <fred@spider.compart.fi> writes:
  15. >>This is driving me nuts.  I can't find a simple way to get the size of a
  16. >>disk file.  This must be possible using the standard C library, but I
  17. >>haven't figured out how!
  18. >
  19. >#include <stdio.h>                                           
  20. >                                                             
  21. >int main(int argc, char *argv[]) {                           
  22. >  FILE *fptr;                                                
  23. >  fpos_t length;                                             
  24. >  fptr=fopen(argv[1],"r");
  25.  
  26. You should check if an argv[1] exists at all...
  27.                                    
  28. >  if (fptr==NULL) {                                          
  29. >    printf("Argh! Can't open '%s' !\n",argv[1]);             
  30. >    exit(-1);                                                
  31.  
  32. #include `stdlib.h> if you need exit(); and, -1 is platform dependent;
  33. the ANSI/ISO defined values are 0, EXIT_SUCCESS and EXIT_FAILURE.
  34.  
  35. >  }                                                          
  36. >  fseek(fptr,0,2);                                           
  37. >  length=ftell(fptr);                                        
  38.  
  39. ftell returns a long, not a fpos_t.  fpos_t's are used by fgetpos()
  40. and fsetpos().  Declare length as a long.
  41.  
  42. >  printf("The size of '%s' is %ld bytes\n",argv[1],length);  
  43.  
  44. The %ld is correct for a long; a fpos_t should be casted to (unsigned
  45. long) and printed as such.
  46.  
  47. >  fclose(fptr);
  48. >  return(0);                                                   
  49. >}                                                            
  50.  
  51. Apart from that, I realize that you missed Q/A 19.12 reading the FAQ
  52. list before posting:
  53.  
  54. 19.12:    How can I find out the size of a file, prior to reading it in?
  55.  
  56. A:    If the "size of a file" is the number of characters you'll be
  57.     able to read from it in C, it is difficult or impossible to
  58.     determine this number exactly).
  59.  
  60.     Under Unix, the stat() call will give you an exact answer.
  61.     Several other systems supply a Unix-like stat() which will give
  62.     an approximate answer.  You can fseek() to the end and then use
  63.     ftell(), but these tend to have the same problems: fstat() is
  64.     not portable, and generally tells you the same thing stat()
  65.     tells you; ftell() is not guaranteed to return a byte count
  66.     except for binary files.  Some systems provide routines called
  67.     filesize() or filelength(), but these are not portable, either.
  68.  
  69.     Are you sure you have to determine the file's size in advance?
  70.     Since the most accurate way of determining the size of a file as
  71.     a C program will see it is to open the file and read it, perhaps
  72.     you can rearrange the code to learn the size as it reads.
  73.  
  74.     References: ANSI Sec. 4.9.9.4; ISO Sec. 7.9.9.4; H&S
  75.     Sec. 15.5.1; PCS Sec. 12 p. 213; POSIX Sec. 5.6.2.
  76. --
  77. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  78. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  79.